var question = {
	listData : new Array(),
	state : [ "作废", "正常" ],
	typeMap : {},
	classes : [ "单选题", "多选题", "判断题", "填空题", "解答题" ],
}
$(function() {

	$("#questionOptions")
			.on(
					"click",
					"a.addOption",
					function() {// 选项添加
						var c = $(this).siblings(".optionCode").val();
						if (validation.processNullData(c) != "") {
							var codeNum = c.charCodeAt() + 1;
							c = String.fromCharCode(codeNum);
						}
						var code = '<input type="text" class="m-wrap optionCode" name="listOption['
								+ (codeNum - 65)
								+ '].optionCode" value="'
								+ c
								+ '" readonly>';
						var cont = '<input type="text" class="m-wrap optionContent" name="listOption['
								+ (codeNum - 65) + '].optionContent">';
						var rem = '<a class="removeOption" href="javascript:;"><i class="icon-remove"></i></a>';
						$(this).parent().after(
								"<div class='options_item'>" + code + cont
										+ rem + "</div>");
						// 添加添加按钮
						var add = ' <a class="addOption" href="javascript:;"><i class="icon-plus"></i></a> ';
						$("#questionOptions .addOption").remove();
						$("#questionOptions .options_item:last .optionContent")
								.after(add);
						var i = $("#questionOptions .options_item").length;
						var radio = '<label for="answerRadio' + i + '_edit">'
								+ c
								+ '</label><input type="radio" id="answerRadio'
								+ i + '_edit" name="answerRadio" value="' + c
								+ '" />';
						var checkbox = '<label for="answerCheckbox'
								+ i
								+ '_edit">'
								+ c
								+ '</label><input type="checkbox" id="answerCheckbox'
								+ i + '_edit" name="answerCheckbox" value="'
								+ c + '" />';
						$("#questionCheckboxGloup").append(checkbox);
						$("#questionRadioGloup").append(radio);
						$(
								"#questionCheckboxGloup input,#questionRadioGloup input")
								.button();

					});
	$("#questionOptions")
			.on(
					"click",
					"a.removeOption",
					function() {// 选项删除
						$(this).parent().remove();
						$(
								"#questionCheckboxGloup input:last,#questionRadioGloup input:last,#questionCheckboxGloup label:last,#questionRadioGloup label:last")
								.remove();
						// 添加添加按钮
						var add = ' <a class="addOption" href="javascript:;"><i class="icon-plus"></i></a> ';
						$("#questionOptions .addOption").remove();
						$("#questionOptions .options_item:last .optionContent")
								.after(add);
						for (var i = 0; i < $("#questionOptions .options_item").length; i++) {
							var code = String
									.fromCharCode("A".charCodeAt() + i);
							$("#questionOptions .optionCode").eq(i).attr(
									"name", "listOption[" + i + "].optionCode")
									.attr("value", code);
							$("#questionOptions .optionContent").eq(i).attr(
									"name",
									"listOption[" + i + "].optionContent");
						}
					});
	$(
			"#stateGloup input,#questionJudgeGloup input,#questionCheckboxGloup input,#questionRadioGloup input")
			.button();
	selectClassesClick();
	findType();
	// 设置导入按钮事件
	$("#btnShowImp")
			.click(
					function() {
						$(".PopupBox").show();
						$("#submitExclFileInfo").prop("action",
								"Question/impQuestion");
						$("#submitExclFileInfo .download").prop("href",
								"files/xlsImportTemplate/Question.xls");
					});
	// 设置导入上传按钮事件
	$("#submitExclFileInfo .upload").bind("click", impData);
	$("#btnExp").click(
			function() {
				toolAction.expDataPage("Question/expQuestion", $(
						"#printPage .p_currentPage").text())
			});
});

function findDataPage(actionName, cpage) {
	if (actionName == null || actionName == "") {
		actionName = "Question/findQuestionPage";
	}
	toolAction.findData(actionName, cpage, "#printPage", findDataPage,
			showListData);
}
/**
 * 查询分页列表
 * 
 * @param list
 */
function showListData(list) {
	var node = "#listForm";
	if (setFileInfo.defaultShowListData(list, node)) {
		return;
	}
	question.listData = list;
	for (var i = 0; i < list.length; i++) {
		var tr = $("<tr onDblClick=showViewData(" + i + ")></tr>");
		tr
				.append("<td class='permissions_delete'><input type='checkbox' class='list-page-content-table-checkboxs' name='ids' value='"
						+ list[i].questionId + "' /></td>");
		tr.append("<td>" + validation.processNullData(list[i].code) + "</td>");
		tr.append("<td>" + question.typeMap[list[i].typeId] + "</td>");
		tr.append("<td>" + question.classes[list[i].classes] + "</td>");
		tr.append("<td>" + validation.processNullData(list[i].subject)
				+ "</td>");

		var answer = list[i].answer;
		if (list[i].classes == "2") {// 判断题
			answer = answer == "0" ? "正确" : "错误";
		}
		tr.append("<td>" + answer + "</td>");
		tr.append("<td>" + question.state[list[i].state] + "</td>");
		var check = "<a class='editIcon' title='查看' href='javascript:;' onclick='showViewData("
				+ i + ")'> <i class='icon-search'></i></a>"; // 查看按钮
		var edit = "<a class='editIcon permissions_update' title='编辑' href='javascript:;' onClick='showEditData("
				+ i + ")'> <i class='icon-edit'></i></a>";// 编辑按钮
		var remove = "<a class='deleteIcon permissions_delete' title='删除' href='javascript:;' onclick=removeData('"
				+ list[i].questionId + "')> <i class='icon-trash'></i></a>";
		tr.append("<td>" + check + edit + remove + "</td>");
		$(node + " tbody").append(tr);

	}
}
function showAddData() {
	$("#submitForm").attr("action", "Question/addQuestion");
	resetData();
}
/**
 * 显示详细信息
 * 
 * @param n
 */
function showViewData(n) {
	resetData();
	var list = question.listData[n];
	$(".editP").hide();
	$(".viewP").show();
	$("#taskView").click();
	setFileInfo.defaultShowViewData(list, "#submitForm");
	$("#state_view").html(question.state[list.state]);
	$("#typeId_view").html(question.typeMap[list.typeId]);
	$("#classes_view").html(question.classes[list.classes]);
	$("#classes_edit").val(list.classes).change();
	setQuestionOption(list);
}
/**
 * 显示修改页面信息
 * 
 * @param n
 */

function showEditData(n) {
	resetData();
	$("#submitForm").attr("action", "Question/updateQuestion");
	var list = question.listData[n];
	$(".viewP").hide();
	$(".editP").show();
	$("#nextSubmit_btn").hide();
	$("#taskEdit").click();
	setFileInfo.defaultShowEditData(list, "#submitForm");
	$("#classes_edit").val(list.classes).change();
	if (list.state == 1) {
		$("#state1_edit").prop("checked", true);
	} else {
		$("#state2_edit").prop("checked", false);
	}
	setQuestionOption(list);
	$("#typeParentDialog .li_" + list.typeId).children(".appendNodeP").click();// 选择当前的类别
}
/**
 * 设置选择项的内容
 * 
 * @param list
 */
function setQuestionOption(list) {
	if (list.classes == "0" || list.classes == "1") {// 单选\多选
		toolAction
				.baseAjax("Question/findQuestionOptionByQuestionId",
						"questionId=" + list.questionId)
				.done(
						function(data) {
							var items = data.baseModel.data;
							var optionsNum = $("#questionOptions .options_item").length;// 页面默认选项数
							if (items.length < optionsNum) {// 如果选项小于4个
								for (var i = items.length; i < optionsNum; i++) {
									$("#questionOptions .removeOption:last")
											.click();
								}
							} else if (items.length > optionsNum) {// 如果选项大于4个
								for (var i = optionsNum; i < items.length; i++) {
									$("#questionOptions .addOption").click();
								}
							}
							for (var i = 0; i < items.length; i++) {
								$(
										"#questionOptions .optionCode[value='"
												+ items[i].optionCode + "']")
										.siblings(".optionContent").val(
												items[i].optionContent);
							}
						});
		if (list.classes == "0") {// 单选
			$("#questionRadioGloup input[value='" + list.answer + "']").click();
		} else {
			var sel = list.answer.split(",");
			for (var i = 0; i < sel.length; i++) {
				$("#questionCheckboxGloup input[value='" + sel[i] + "']")
						.click();
			}
		}
	} else if (list.classes == "2") {// 判断
		if (list.answer == "0") {
			$("#answerJudge1_edit").click();
		} else {
			$("#answerJudge2_edit").click();
		}
	} else {// 填空、解答

	}
}
/**
 * 提交数据
 */
function submitEditData(isNext) {
	if (checkData()) {
		var actionName = $("#submitForm").attr("action");
		var formData = $("#submitForm").serialize();
		toolAction.baseXHR(actionName, formData).done(function(data) {
			if (data.baseModel.resultFlag > 0) {
				alert(data.baseModel.message);
			} else {
				if (isNext == 1) {// 继续添加
					showAddData();
				} else {
					$("#taskList").click();// 返回列表
				}
			}

		});
	}
}
/**
 * 删除数据
 * 
 * @param id
 */
function removeData(id) {
	var nodeLocation = "#listForm";
	var actionName = "Question/deleteQuestion";
	// 设置删除请求，删除的id，批量删除时复选框所在节点，请求路径，查询方法
	toolAction.removeData(id, nodeLocation, actionName, findDataPage);
}
/**
 * 验证提交数据
 * 
 * @returns {Boolean}
 */
function checkData() {
	var flag = true;
	var str = "";
	var typeId_edit = $("#typeId_view input");
	if (typeId_edit.length < 1 || typeId_edit.val() == "") {
		str += "请选择题目分类\n";
		typeId_edit.focus();
	}
	var subject_edit = $("#subject_edit");// 提干
	if (!validation.isNull(subject_edit.val())) {
		str += "提干不能为空\n";
		subject_edit.focus();
	}
	var classes_edit = $("#classes_edit");// 类型
	if (classes_edit.val() == "0") {// 单选
		if ($("#questionRadioGloup input:checked").length < 1) {
			str += "请选择答案\n";
		}
	} else if (classes_edit.val() == "1") {// 多选
		if ($("#questionCheckboxGloup input:checked").length < 1) {
			str += "请选择答案\n";
		}
	} else if (classes_edit.val() == "2") {// 判断
		if ($("#questionJudgeGloup input:checked").length < 1) {
			str += "请选择答案\n";
		}
	} else {// 填空、解答
		var answer_edit = $("#answer_edit");
		if (!validation.isNull(answer_edit.val())) {
			str += "请输入答案\n";
			answer_edit.focus();
		}
	}
	if (classes_edit.val() == "0" || classes_edit.val() == "1") {
		$.each($("#questionOptions .optionContent"), function() {
			if ($(this).val() == "") {
				str += "请输入选项内容\n";
				$(this).focus();
				return false;
			}
		});

	}
	if (str.length > 0) {
		flag = false;
		alert(str);
	}
	return flag;
}
/**
 * 重置数据
 */
function resetData() {
	$(".viewP").hide();
	$(".editP").show();
	$("#submitForm input[type='text'],#submitForm textarea").val("");
	$("#state_edit").prop("checked", true);// 激活

	$("#questionOptions .options_item .removeOption").click();// 删除其他选项
	$("#questionOptions .options_item.item_index .optionCode").val("A");
	for (var i = 0; i < 3; i++) {
		$("#questionOptions .options_item:last .addOption").click();
	}
	$("#state1_edit").click();// 状态
	if ($("#classes_edit").val() == "0"
			&& $("#optionContent_optionsGloup").css("display") == "none") {
		$("#classes_edit").val("0").change();
	}

	$("#questionCheckboxGloup input:checked").click();// 复选框全部不选中
	$("#typeParentDialog").siblings(".ui-dialog-buttonpane").find("button").eq(
			1).click();
}
/**
 * 返回列表
 */
function returnData() {
	setFileInfo.defaultReturnList();
}
/**
 * 快速查询
 */
function searchqData() {
	setFileInfo.defaultSearchParms("#searchqDataBox");
	findDataPage();// 查询信息

}
/**
 * 高级查询
 */
function searchData() {
	setFileInfo.defaultSearchParms("#searchDataBox");
	advancedQuery.pageSize = $("#pageSize_search").val();
	findDataPage();// 查询信息
}
/**
 * 选择题目类型事件
 */
function selectClassesClick() {
	$("#classes_edit")
			.change(
					function() {
						var val = $(this).val();
						$(".optionsGloup").hide();// 对应下拉框显示的选题答案框
						if (val == "0") {
							$(
									"#optionContent_optionsGloup,#answerRadio_optionsGloup")
									.show();// 选项框
						} else if (val == "1") {
							$(
									"#optionContent_optionsGloup,#answerCheckbox_optionsGloup")
									.show();
						} else if (val == "2") {
							$("#answerJudge_optionsGloup").show();
						} else {
							$("#answer_optionsGloup").show();
						}
					});
}
/**
 * 查询分类
 */
function findType() {
	$.each(question.classes, function(i, v) {
		$("#classes_search").append(
				"<option value='" + i + "'>" + v + "</option>");
	});

	toolAction.baseAjax("QuestionType/findQuestionType", "").done(
			function(data) {
				var list = data.baseModel.data;
				setSelectBox.typeParentDialog("#typeParentDialog",
						"#typeId_view", "typeId", list, "typeId", "typeName",
						"parentTypeId");
				if (list != null) {
					for (var i = 0; i < list.length; i++) {
						$("#typeId_search,#typeId_searchq").append(
								"<option value='" + list[i].typeId + "'>"
										+ list[i].typeName + "</option");
						question.typeMap[list[i].typeId] = list[i].typeName;
					}
				}

			});
}
/**
 * 导入
 */
function impData() {
	toolAction.impData("#submitExclFileInfo", findDataPage);
}
function showTypeDialog() {
	$("#typeParentDialog").dialog("open");
}